package com.apes.scm.masterdata.product.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/***
 * 方位图菜单
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = OrientationDiagramMenu.CONST_TABLE_NAME)
public class OrientationDiagramMenu extends Model {

    public static final String CONST_TABLE_NAME = "orientation_diagram_menu";


    @Id
    @Column(name = "id")
    @Comment("编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "方位图菜单")
    private Integer id;

    @Name
    @Comment("名称")
    @Column(name = "name")
    private String name;

    @Comment("父级目录")
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private OrientationDiagramMenu parent;

    @Comment("子目录")
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<OrientationDiagramMenu> children;


    @File
    @Lob
    @Comment("图片")
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "image", columnDefinition = "CLOB")
    private String image;


    @Column(name = "is_last_stage")
    @Comment("末级标记")
    private boolean lastStage;  //末级标记

    /* 数层级名称 */
    @Name
    @Column(name = "path_name", length = 4000)
    @Comment("树层级结构")
    private String pathName;

    public String localPathName() {
        String path = null;
        if (parent != null) path = parent.localPathName();
        return path != null ? (path + "/" + name) : (name + "");
    }


    /*树层级结构*/
    @Column(name = "path", length = 4000)
    @Comment("树层级结构")
    private String path;

    public String localPath() {
        String path = null;
        if (parent != null) path = parent.localPath();
        return path != null ? (path + "/" + id) : (id + "");
    }

    @OneToMany(mappedBy = "menu", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("id asc")
    private List<OrientationDiagramCategory> categories = new ArrayList<>();


    public List<OrientationDiagramCategory> getCategories() {

        List<OrientationDiagramCategory> tempList  = unique(categories); // 过滤相同配件名称的数据

        categories.clear();
        categories.addAll(tempList);

        for (OrientationDiagramCategory item : categories) {
            item.setMenu(this);
        }
        return categories;
    }

    // 过滤相同配件名称的数据
    private List<OrientationDiagramCategory> unique(List<OrientationDiagramCategory> categories) {

        Map<Category, List<OrientationDiagramCategory>> groupByCategory =
                categories.stream().collect(Collectors.groupingBy(OrientationDiagramCategory::getCategory));

        return groupByCategory.entrySet().stream().
                map(categoryListEntry -> categoryListEntry.getValue().subList(0, 1))
                .reduce(new ArrayList<>(), (a1, b1) -> {
                    a1.addAll(b1);
                    return a1;
                });
    }


}
